totSteps <- sum(resultChi$observed)
chi <- resultChi$statistic
pval <- resultChi$p.value
return(list(total = total,
favourites = favourites,
totSteps = totSteps,
chisquare = chi[[1]],
pvalue = pval))
}
# Chunk 3: traj00006
traj <- "traj00006"
ii <- interact(traj)
ii
resultChi <- chisq.test(ii[2])
resultTraj00006 <-summtraj(resultChi)
resultTraj00006
pl(ii)
svg(filename = "graphs/fig-traj00006.svg")
pl(ii)
dev.off()
# Chunk 4: traj00007
traj <- "traj00007"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00007 <-summtraj(resultChi)
resultTraj00007
pl(ii)
svg(filename = "graphs/fig-traj00007.svg")
pl(ii)
dev.off()
# Chunk 5: traj00012
traj <- "traj00012"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00012 <-summtraj(resultChi)
resultTraj00012
pl(ii)
svg(filename = "graphs/fig-traj00012.svg")
pl(ii)
dev.off()
# Chunk 6: traj00013
traj <- "traj00013"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00013 <-summtraj(resultChi)
resultTraj00013
pl(ii)
svg(filename = "graphs/fig-traj00013.svg")
pl(ii)
dev.off()
# Chunk 7: traj00095
traj <- "traj00095"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00095 <-summtraj(resultChi)
resultTraj00095
pl(ii)
svg(filename = "graphs/fig-traj00095.svg")
pl(ii)
dev.off()
# Chunk 8: traj00096
traj <- "traj00096"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00096 <-summtraj(resultChi)
resultTraj00096
pl(ii)
svg(filename = "graphs/fig-traj00096.svg")
pl(ii)
dev.off()
# Chunk 9: traj00100
traj <- "traj00100"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00100 <-summtraj(resultChi)
resultTraj00100
pl(ii)
svg(filename = "graphs/fig-traj00100.svg")
pl(ii)
dev.off()
# Chunk 10: traj00130
traj <- "traj00130"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00130 <-summtraj(resultChi)
resultTraj00130
pl(ii)
svg(filename = "graphs/fig-traj00130.svg")
pl(ii)
dev.off()
# Chunk 11: traj00143
traj <- "traj00143"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00143 <-summtraj(resultChi)
resultTraj00143
pl(ii)
svg(filename = "graphs/fig-traj00143.svg")
pl(ii)
dev.off()
# Chunk 12: traj00144
traj <- "traj00144"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00144 <-summtraj(resultChi)
resultTraj00144
pl(ii)
svg(filename = "graphs/fig-traj00144.svg")
pl(ii)
dev.off()
# Chunk 13: traj00145
traj <- "traj00145"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00145 <-summtraj(resultChi)
resultTraj00145
pl(ii)
svg(filename = "graphs/fig-traj00145.svg")
pl(ii)
dev.off()
# Chunk 14
traj <- sort(unique(dt$trajectory))
groupSize <- c(resultTraj00006$total,
resultTraj00007$total,
resultTraj00012$total,
resultTraj00013$total,
resultTraj00095$total,
resultTraj00096$total,
resultTraj00100$total,
resultTraj00130$total,
resultTraj00143$total,
resultTraj00144$total,
resultTraj00145$total
)
favourites <- c(resultTraj00006$favourites,
resultTraj00007$favourites,
resultTraj00012$favourites,
resultTraj00013$favourites,
resultTraj00095$favourites,
resultTraj00096$favourites,
resultTraj00100$favourites,
resultTraj00130$favourites,
resultTraj00143$favourites,
resultTraj00144$favourites,
resultTraj00145$favourites
)
steps <- c(resultTraj00006$totSteps,
resultTraj00007$totSteps,
resultTraj00012$totSteps,
resultTraj00013$totSteps,
resultTraj00095$totSteps,
resultTraj00096$totSteps,
resultTraj00100$totSteps,
resultTraj00130$totSteps,
resultTraj00143$totSteps,
resultTraj00144$totSteps,
resultTraj00145$totSteps
)
chisq <- c(resultTraj00006$chisquare,
resultTraj00007$chisquare,
resultTraj00012$chisquare,
resultTraj00013$chisquare,
resultTraj00095$chisquare,
resultTraj00096$chisquare,
resultTraj00100$chisquare,
resultTraj00130$chisquare,
resultTraj00143$chisquare,
resultTraj00144$chisquare,
resultTraj00145$chisquare
)
pval <- c(resultTraj00006$pvalue,
resultTraj00007$pvalue,
resultTraj00012$pvalue,
resultTraj00013$pvalue,
resultTraj00095$pvalue,
resultTraj00096$pvalue,
resultTraj00100$pvalue,
resultTraj00130$pvalue,
resultTraj00143$pvalue,
resultTraj00144$pvalue,
resultTraj00145$pvalue
)
summdf <- data.frame(traj,groupSize,fav=favourites, steps=round(steps,0), chisq, pval)
nestID <- read.table("nestIDtraj.csv", h=T)
summTab <- merge(nestID,summdf,by="traj")
library(xtable)
print(xtable(summTab,
display = c("s","s","s","s","s","s","g","g")),
math.style.exponents=TRUE
)
write.csv(summTab, file = "../dat/summaryFavourites.csv", row.names = FALSE)
favourites/groupSize
mean(favourites/groupSize)
# Chunk 15: favouritismLine
m1<-glm(favourites~groupSize)
m0<-glm(favourites~offset(0+groupSize))
AIC(m0,m1)
anova(m0,m1, test="F")
pfav <- anova(m0,m1, test="F")$'Pr(>F)'[2]
library(scales)
# digits = 3 is the default but I am setting it here to be explicit,
# and draw attention to the fact this is different than the formatC
# solution.
pfav <- scientific(pfav, digits = 3)
pfav
anova(m1,test="F")
coef(m1)
plot(favourites~jitter(groupSize,factor=2)
,lwd = 3
,cex = 3
,pch = 21
,xlab = "number of termites in arena"
,ylab = "number of favourites"
)
curve(coef(m1)[1]+coef(m1)[2]*x, add=T,lwd=3)
curve(0+1*x, add = T, col = "red", lwd = 3, lty=2)
eq0 <- c('y ~ x')
eq0
text(5.7,7,paste("no favouritism:", eq0,sep='\n'),col="red", adj=1)
eq1 <- paste('y ~'
,round(coef(m1)[1],3)
,'+'
,round(coef(m1)[2],3)
, 'x'
)
eq1
pval <- paste('P = ', pfav)
text(12,4, paste(
'favouritism:'
,eq1
,pval
,sep='\n'
)
)
# Chunk 16
dt
# Chunk 1
setwd("./")
dt<-read.table("../dat/newdata.dat",sep=",") # termite1 changed to termite01, etc
names(dt)[1] <- "trajectory"
names(dt)[2]<- "focalTermite"
names(dt)[3]<- "targetTermite"
names(dt)[4]<- "steps"
summary(dt)
# Chunk 2
interact <- function(traj){ # interactions with target termite
ss <- subset(dt,trajectory==traj)
ss <- droplevels(ss)
##
## the total number of steps here considers readings
## from termite01 > termite02 as well as termite02 > termite01.
## We should make it as the mean of both cases.
totSteps <- sum(ss$steps)
interactions <- aggregate(ss$steps,list(ss$targetTermite),mean)
names(interactions)[1] <- "targetTermite"
names(interactions)[2] <- "steps"
return(interactions)
}
pl<-function(interactions)
{pval <- round(chisq.test(interactions[2])$p.value,4)
expected <- chisq.test(interactions[2])$expected
barplot(interactions$steps,
names.arg = interactions$targetTermite,
las=2,
main=paste(traj, " p = ",pval ),
ylab="time-steps",
#xlab="termite ID",
ylim=c(0,1.1*max(interactions[2]))
)
abline(h=expected[1],col="red")
}
summtraj <- function(resultChi){
expected <- resultChi$expected
observed <- resultChi$observed
favourites <- sum(observed>expected)
total <- length(resultChi$observed)
totSteps <- sum(resultChi$observed)
chi <- resultChi$statistic
pval <- resultChi$p.value
return(list(total = total,
favourites = favourites,
totSteps = totSteps,
chisquare = chi[[1]],
pvalue = pval))
}
# Chunk 3: traj00006
traj <- "traj00006"
ii <- interact(traj)
ii
resultChi <- chisq.test(ii[2])
resultTraj00006 <-summtraj(resultChi)
resultTraj00006
pl(ii)
svg(filename = "graphs/fig-traj00006.svg")
pl(ii)
dev.off()
# Chunk 4: traj00007
traj <- "traj00007"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00007 <-summtraj(resultChi)
resultTraj00007
pl(ii)
svg(filename = "graphs/fig-traj00007.svg")
pl(ii)
dev.off()
# Chunk 5: traj00012
traj <- "traj00012"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00012 <-summtraj(resultChi)
resultTraj00012
pl(ii)
svg(filename = "graphs/fig-traj00012.svg")
pl(ii)
dev.off()
# Chunk 6: traj00013
traj <- "traj00013"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00013 <-summtraj(resultChi)
resultTraj00013
pl(ii)
svg(filename = "graphs/fig-traj00013.svg")
pl(ii)
dev.off()
# Chunk 7: traj00095
traj <- "traj00095"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00095 <-summtraj(resultChi)
resultTraj00095
pl(ii)
svg(filename = "graphs/fig-traj00095.svg")
pl(ii)
dev.off()
# Chunk 8: traj00096
traj <- "traj00096"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00096 <-summtraj(resultChi)
resultTraj00096
pl(ii)
svg(filename = "graphs/fig-traj00096.svg")
pl(ii)
dev.off()
# Chunk 9: traj00100
traj <- "traj00100"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00100 <-summtraj(resultChi)
resultTraj00100
pl(ii)
svg(filename = "graphs/fig-traj00100.svg")
pl(ii)
dev.off()
# Chunk 10: traj00130
traj <- "traj00130"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00130 <-summtraj(resultChi)
resultTraj00130
pl(ii)
svg(filename = "graphs/fig-traj00130.svg")
pl(ii)
dev.off()
# Chunk 11: traj00143
traj <- "traj00143"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00143 <-summtraj(resultChi)
resultTraj00143
pl(ii)
svg(filename = "graphs/fig-traj00143.svg")
pl(ii)
dev.off()
# Chunk 12: traj00144
traj <- "traj00144"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00144 <-summtraj(resultChi)
resultTraj00144
pl(ii)
svg(filename = "graphs/fig-traj00144.svg")
pl(ii)
dev.off()
# Chunk 13: traj00145
traj <- "traj00145"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00145 <-summtraj(resultChi)
resultTraj00145
pl(ii)
svg(filename = "graphs/fig-traj00145.svg")
pl(ii)
dev.off()
# Chunk 14
traj <- sort(unique(dt$trajectory))
groupSize <- c(resultTraj00006$total,
resultTraj00007$total,
resultTraj00012$total,
resultTraj00013$total,
resultTraj00095$total,
resultTraj00096$total,
resultTraj00100$total,
resultTraj00130$total,
resultTraj00143$total,
resultTraj00144$total,
resultTraj00145$total
)
favourites <- c(resultTraj00006$favourites,
resultTraj00007$favourites,
resultTraj00012$favourites,
resultTraj00013$favourites,
resultTraj00095$favourites,
resultTraj00096$favourites,
resultTraj00100$favourites,
resultTraj00130$favourites,
resultTraj00143$favourites,
resultTraj00144$favourites,
resultTraj00145$favourites
)
steps <- c(resultTraj00006$totSteps,
resultTraj00007$totSteps,
resultTraj00012$totSteps,
resultTraj00013$totSteps,
resultTraj00095$totSteps,
resultTraj00096$totSteps,
resultTraj00100$totSteps,
resultTraj00130$totSteps,
resultTraj00143$totSteps,
resultTraj00144$totSteps,
resultTraj00145$totSteps
)
chisq <- c(resultTraj00006$chisquare,
resultTraj00007$chisquare,
resultTraj00012$chisquare,
resultTraj00013$chisquare,
resultTraj00095$chisquare,
resultTraj00096$chisquare,
resultTraj00100$chisquare,
resultTraj00130$chisquare,
resultTraj00143$chisquare,
resultTraj00144$chisquare,
resultTraj00145$chisquare
)
pval <- c(resultTraj00006$pvalue,
resultTraj00007$pvalue,
resultTraj00012$pvalue,
resultTraj00013$pvalue,
resultTraj00095$pvalue,
resultTraj00096$pvalue,
resultTraj00100$pvalue,
resultTraj00130$pvalue,
resultTraj00143$pvalue,
resultTraj00144$pvalue,
resultTraj00145$pvalue
)
summdf <- data.frame(traj,groupSize,fav=favourites, steps=round(steps,0), chisq, pval)
nestID <- read.table("nestIDtraj.csv", h=T)
summTab <- merge(nestID,summdf,by="traj")
library(xtable)
print(xtable(summTab,
display = c("s","s","s","s","s","s","g","g")),
math.style.exponents=TRUE
)
write.csv(summTab, file = "summaryFavourites.csv", row.names = FALSE)
favourites/groupSize
mean(favourites/groupSize)
# Chunk 15: favouritismLine
m1<-glm(favourites~groupSize)
m0<-glm(favourites~offset(0+groupSize))
AIC(m0,m1)
anova(m0,m1, test="F")
pfav <- anova(m0,m1, test="F")$'Pr(>F)'[2]
library(scales)
# digits = 3 is the default but I am setting it here to be explicit,
# and draw attention to the fact this is different than the formatC
# solution.
pfav <- scientific(pfav, digits = 3)
pfav
anova(m1,test="F")
coef(m1)
plot(favourites~jitter(groupSize,factor=2)
,lwd = 3
,cex = 3
,pch = 21
,xlab = "number of termites in arena"
,ylab = "number of favourites"
)
curve(coef(m1)[1]+coef(m1)[2]*x, add=T,lwd=3)
curve(0+1*x, add = T, col = "red", lwd = 3, lty=2)
eq0 <- c('y ~ x')
eq0
text(5.7,7,paste("no favouritism:", eq0,sep='\n'),col="red", adj=1)
eq1 <- paste('y ~'
,round(coef(m1)[1],3)
,'+'
,round(coef(m1)[2],3)
, 'x'
)
eq1
pval <- paste('P = ', pfav)
text(12,4, paste(
'favouritism:'
,eq1
,pval
,sep='\n'
)
)
# Chunk 16
dt
